#include "clientwidget.h"
#include "ui_clientwidget.h"

ClientWidget::ClientWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ClientWidget)
{
    ui->setupUi(this);
    //qDebug() << "emitting loadLog signal";
    //emit loadLogs(owner);
}

ClientWidget::~ClientWidget()
{
    delete ui;
}

void ClientWidget::addNewEntrySuccessful(const QString &timeStamp)
{
    qDebug() << "New log should be showing up";
    ui->lineEdit->setEnabled(true);
    ui->newEntryButton->setEnabled(true);
    QString newEntry = QString(timeStamp).append(" ").append(ui->lineEdit->text());
    ui->listWidget->addItem(newEntry);
    ui->lineEdit->clear();
}


void ClientWidget::addNewEntryError(const QString &errorMessage)
{
    ui->lineEdit->setEnabled(true);
    ui->newEntryButton->setEnabled(true);
    QMessageBox::information(this, tr("New Log Entry Request Failed"), errorMessage);
}


void ClientWidget::readLog(LogEntry *entry)
{
    QString line = entry->getTimeStamp().toString("yyyy-MM-dd hh:mm:ss");
    line.append(" ");
    line.append(entry->getLog());
    qDebug() << line;
    ui->listWidget->addItem(line);
}

void ClientWidget::readLogError(const QString &errorMessage)
{
    QMessageBox::information(this, tr("Loading Log failed"),
                                      errorMessage);
}

void ClientWidget::on_newEntryButton_clicked()
{

    if (ui->lineEdit->text().size() > 255 || ui->lineEdit->text().size() < 1) {
        QMessageBox::information(this, tr("Exceeding 255 characters!"), tr("Try again"));
    }

    LogEntry *newentry = new LogEntry();
    newentry->setLog(ui->lineEdit->text());
    newentry->setOwner(owner);
    qDebug() << newentry->getOwner();
    ui->lineEdit->setEnabled(false);
    ui->newEntryButton->setEnabled(false);

    emit newEntry(newentry);

}

void ClientWidget::setOwner(const QString &newOwner)
{
    owner = newOwner;
}

void ClientWidget::loadLogList()
{
    qDebug() << "emittign loadLogs signal";
    QStringList *list = new QStringList;
    list->append(owner);
    emit loadLogs(list);
}

void ClientWidget::loadUsersBox(UserData *user)
{
    emit loadUsers(user);
}

void ClientWidget::userList(QStringList *list)
{
    ui->comboBox->addItem(owner); //owner at the top
    foreach(QString username, *list) {
        ui->comboBox->addItem(username);
    }
}

void ClientWidget::userListError(const QString &errorMessage)
{
    QMessageBox::information(this, tr("Loading comboBox failed"), errorMessage);
}

void ClientWidget::on_comboBox_activated(const QString &username)
{
    if (username != owner) {
        ui->lineEdit->setEnabled(false);
        ui->newEntryButton->setEnabled(false);
    } else {
        ui->lineEdit->setEnabled(true);
        ui->newEntryButton->setEnabled(true);
    }
    ui->listWidget->clear();
    //if (username == owner)
    //    return; //don't do anything.
    qDebug() << username;
    // Request for specific log

    QStringList *list = new QStringList;
    list->append(username);
    emit loadLogs(list);
}
